package Frame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Window {
    private JFrame main;
    private JPanel login;
    private JPanel register, operator;
    private JTextField account, username;
    private JPasswordField password, registerPasswordField;

    public Window() {
        main = new JFrame("界面跳转示例");
        main.setSize(400, 300);
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setLocationRelativeTo(null); // 居中显示

        // 创建登陆面板
        createLogin();
        // 创建注册面板
        createRegister();
        // 创建操作面板
        createOperation();

        showPanel(login); // 使用自定义方法显示面板
    }

    // 自定义方法：显示指定面板
    private void showPanel(JPanel panel) {
        main.getContentPane().removeAll();
        main.getContentPane().add(panel);
        main.revalidate();
        main.repaint();
    }

    private void createLogin() {
        login = new JPanel();
        login.setLayout(new GridLayout(5, 2)); // 修改为5行2列布局

        JLabel accountLabel = new JLabel("账号:");
        account = new JTextField();
        JLabel passwordLabel = new JLabel("密码:");
        password = new JPasswordField();
        
        JButton loginButton = new JButton("登录");
        JButton registerButton = new JButton("注册");
        JButton quitButton = new JButton("退出");

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String account1 = account.getText();
                char[] passwordChars = password.getPassword();
                String password1 = new String(passwordChars);
                
                if ("admin".equals(account1) && "123456".equals(password1)) {
                    JOptionPane.showMessageDialog(main, "登陆成功");
                    showPanel(operator); // 使用自定义方法切换面板
                } else {
                    JOptionPane.showMessageDialog(main, "登陆无效，请检查账号密码", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel(register); // 使用自定义方法切换面板
            }
        });

        quitButton.addActionListener(e -> System.exit(0)); // 退出程序

        // 添加组件到面板
        login.add(accountLabel);
        login.add(account);
        login.add(passwordLabel);
        login.add(password);
        login.add(new JLabel()); // 占位
        login.add(loginButton);
        login.add(new JLabel()); // 占位
        login.add(registerButton);
        login.add(new JLabel()); // 占位
        login.add(quitButton);
    }

    private void createRegister() {
        register = new JPanel();
        // 使用GridBagLayout布局，方便精确控制组件位置和大小
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        register.setLayout(gbl);

        JLabel accountLabel = new JLabel("请您输入账号");
        JTextField accountField = new JTextField();
        JLabel passwordLabel = new JLabel("请您输入密码");
        JPasswordField passwordField = new JPasswordField();
        JLabel confirmPasswordLabel = new JLabel("输入确认密码");
        JPasswordField confirmPasswordField = new JPasswordField();
        JLabel nameLabel = new JLabel("请您输入姓名");
        JTextField nameField = new JTextField();
        JLabel departmentLabel = new JLabel("请您选择部门");
        String[] departments = {"财务部", "销售部", "技术部"};
        JComboBox<String> departmentComboBox = new JComboBox<>(departments);

        JButton registerButton = new JButton("注册");
        JButton loginButton = new JButton("登录");
        JButton quitButton = new JButton("退出");

        // 添加组件到面板，设置GridBagConstraints参数
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbl.setConstraints(accountLabel, gbc);
        register.add(accountLabel);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbl.setConstraints(accountField, gbc);
        register.add(accountField);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbl.setConstraints(passwordLabel, gbc);
        register.add(passwordLabel);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbl.setConstraints(passwordField, gbc);
        register.add(passwordField);

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbl.setConstraints(confirmPasswordLabel, gbc);
        register.add(confirmPasswordLabel);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbl.setConstraints(confirmPasswordField, gbc);
        register.add(confirmPasswordField);

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbl.setConstraints(nameLabel, gbc);
        register.add(nameLabel);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbl.setConstraints(nameField, gbc);
        register.add(nameField);

        gbc.gridx = 0;
        gbc.gridy = 4;
        gbl.setConstraints(departmentLabel, gbc);
        register.add(departmentLabel);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbl.setConstraints(departmentComboBox, gbc);
        register.add(departmentComboBox);

        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.gridwidth = 1;
        gbl.setConstraints(registerButton, gbc);
        register.add(registerButton);

        gbc.gridx = 1;
        gbc.gridy = 5;
        gbl.setConstraints(loginButton, gbc);
        register.add(loginButton);

        gbc.gridx = 2;
        gbc.gridy = 5;
        gbl.setConstraints(quitButton, gbc);
        register.add(quitButton);

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 注册逻辑，包含密码一致性等验证
                String accountText = accountField.getText();
                char[] passwordChars = passwordField.getPassword();
                char[] confirmPasswordChars = confirmPasswordField.getPassword();
                String nameText = nameField.getText();
                String department = (String) departmentComboBox.getSelectedItem();
                String password = new String(passwordChars);
                String confirmPassword = new String(confirmPasswordChars);
                if (accountText.isEmpty() || password.isEmpty() || confirmPassword.isEmpty() || nameText.isEmpty()) {
                    JOptionPane.showMessageDialog(main, "账号、密码、确认密码、姓名不能为空", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if (!password.equals(confirmPassword)) {
                    JOptionPane.showMessageDialog(main, "两次输入的密码不一致", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                JOptionPane.showMessageDialog(main, "注册成功");
                // 其他注册成功后的逻辑，如跳转等
            }
        });

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 跳转到登录界面逻辑
                showPanel(login);
            }
        });

        quitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    }

    private void createOperation() {
        operator = new JPanel();
        JLabel operationLabel = new JLabel("操作界面");
        JButton logoutButton = new JButton("退出登录");
        
        logoutButton.addActionListener(e -> {
            showPanel(login); // 使用自定义方法切换面板
            
            // 清空登录表单
            account.setText("");
            password.setText("");
        });
        
        operator.add(operationLabel);
        operator.add(logoutButton);
        operator.setVisible(false);
    }

    public static void main(String[] args) {
        // 使用事件调度线程创建GUI
        SwingUtilities.invokeLater(() -> {
            Window window = new Window();
            window.main.setVisible(true);
        });
    }
}